package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.data.MyGroupingData;
import com.banzhi.shop.model.CallBack.MyGroupingCallBack;
import com.banzhi.shop.model.IMyGroupingModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2018/7/23.
 */

public class MyGroupingImpl implements IMyGroupingModel {
    @Override
    public void getMyGroupingData(String type, String page, String pagesize, final MyGroupingCallBack callBack) {
        //这里写死
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.MyGroupingURL);//这里写死
      //  String url ="http://preview.banzhishenghuo.com/shop/groupshare/getmyinfo?token=92f3db59ae0e1cf95db120e2c508c0cf&type=0&page=1&pagesize=20";
      //  RequestParams params = new RequestParams(url);//这里写死
        params.addBodyParameter("type", 0+"" );
        params.addBodyParameter("page", 1+"" );
        params.addBodyParameter("page", 20+"" );
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("MyGroupingImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtils.e("result:", result.toString());
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        MyGroupingData myGroupingData = gson.fromJson(result, MyGroupingData.class);

                        callBack.onGetMyGroupingDataSuccess(myGroupingData);
                    }else {
                        callBack.onGetMyGroupingDataError(jsonObject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("OrderManagementImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(106)");

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();

            }
        });
    }
}
